package com.lg.erp.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 日志切面类
 */
@Aspect
@Component
public class LogAspect {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("execution(* com.lg.erp..*.*(..))")
    public void pc(){
    }

    @Around("pc()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //输出方法名称
        logger.debug("Method name：" + joinPoint.getSignature().getName());
        //输出参数
        Object[] args = joinPoint.getArgs();
        for(int i = 0;i < args.length;i++){
            logger.debug(String.format("Args[%d]:%s ",i,args[i]));
        }
        long begin = System.currentTimeMillis();
        //执行方法
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        logger.debug("Cost time:"+(end - begin));
        logger.debug("Return Value:" + result);
        return result;
    }

    @AfterThrowing(value = "pc()",throwing = "throwable")
    public void throwWatch(Throwable throwable){
        logger.debug(String.format("Method:,throws:%s",throwable.getMessage()),throwable);
    }
}
